package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.po.tabletags.TableTagsPo;
import com.bwda.dsrs.filecenter.domain.condition.tabletags.TableTagsCondition;
import com.bwda.dsrs.filecenter.form.tabletags.TableTagsQueryForm;
import com.bwda.dsrs.filecenter.form.tabletags.TableTagsUpdateForm;
import com.bwda.dsrs.filecenter.form.tabletags.TableTagsCreateForm;
import com.bwda.dsrs.filecenter.vo.tabletags.TableTagsVo;
import com.bwda.dsrs.filecenter.service.TableTagsService;

import java.util.List;
import java.util.ArrayList;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;

@RestController
@RequestMapping("/b/tabletags")
@Api(value = "/b/tabletags", description = "表的标签")
public class TableTagsController extends BaseController {

	@Autowired
	private TableTagsService tableTagsService;

	@ApiOperation(value = "查询表的标签",notes = "查询表的标签",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<TableTagsVo> query(@ApiParam(value = "ID", required = true)@RequestParam Integer tableTagId) throws BusinessServiceException {
		TableTagsPo po = tableTagsService.queryWithValid(tableTagId);
		TableTagsVo vo = CopyUtil.transfer(po, TableTagsVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询表的标签数量",notes = "查询表的标签数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid TableTagsQueryForm form) throws BusinessServiceException {
		TableTagsCondition condition = this.getConditionByQueryForm(form);
		int count = tableTagsService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询表的标签列表",notes = "查询表的标签列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<TableTagsVo>> queryList(@ModelAttribute@Valid TableTagsQueryForm form) throws BusinessServiceException {
		TableTagsCondition condition = this.getConditionByQueryForm(form);
		List<TableTagsPo> poList = tableTagsService.queryList(condition);
		List<TableTagsVo> voList = CopyUtil.transfer(poList, TableTagsVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询表的标签列表(带分页)",notes = "查询表的标签列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<TableTagsVo>> queryPageList(@ModelAttribute@Valid TableTagsQueryForm form) throws BusinessServiceException {
		TableTagsCondition condition = this.getConditionByQueryForm(form);
		List<TableTagsVo> voList = new ArrayList<>();
		int count = tableTagsService.queryCount(condition);
		if (count > 0) {
			List<TableTagsPo> poList = tableTagsService.queryList(condition);
			voList = CopyUtil.transfer(poList, TableTagsVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增表的标签",notes = "新增表的标签",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<TableTagsVo> add(@ModelAttribute@Valid TableTagsCreateForm form) throws BusinessServiceException {
		TableTagsPo po = CopyUtil.transfer(form, TableTagsPo.class);
		po.setTableTagId(RandomUtils.nextInt());
		tableTagsService.insert(po);
		TableTagsVo vo = CopyUtil.transfer(po, TableTagsVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改表的标签",notes = "修改表的标签",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid TableTagsUpdateForm form) throws BusinessServiceException {
		TableTagsPo po = CopyUtil.transfer(form, TableTagsPo.class);
		tableTagsService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除表的标签",notes = "删除表的标签",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "ID", required = true)@RequestParam Integer tableTagId) throws BusinessServiceException {
		tableTagsService.delete(tableTagId);
		return getSuccessResult();
	}

	/**
	 * TableTagsQueryForm转换为TableTagsCondition
	 *
	 * @param form
	 * @return
	 */
	private TableTagsCondition getConditionByQueryForm(TableTagsQueryForm form) {
		TableTagsCondition condition = CopyUtil.transfer(form, TableTagsCondition.class);
		return condition;
	}

}